import {FenceGroup} from "../models/fence-group";
import {Judger} from "../models/Judger";
import {Spu} from "../../models/spu";
import {Cell} from "../models/cell";
import {Cart} from "../../models/cart";
import {showToast} from "../../utils/ui";

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        spu: Object,
        orderWay: String
    },

    /**
     * 组件的初始数据
     */
    data: {
        judger: Object,
        previewImg: String,
        title: String,
        currentSkuCount: Cart.SKU_MIN_COUNT
    },

    observers: {
        'spu': function (spu) {
            if (!spu) return
            const noSpec = Spu.isNoSpec(spu)
            if (noSpec) {
                this.processNoSpec(spu)
            } else {
                this.processHasSpec(spu)
            }
            this.triggerSpecEvent()
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 处理无规格
        processNoSpec(spu) {
            this.setData({
                noSpec: true
            })
            this.bindSkuData(spu.sku_list[0])
            this.setStockStatus(spu.sku_list[0].stock)
        },
        processHasSpec(spu) {
            const fenceGroup = new FenceGroup(spu);
            fenceGroup.initFences()
            const judger = new Judger(fenceGroup)
            this.data.judger = judger

            const defaultSku = fenceGroup.getDefaultSku()
            if (defaultSku) {
                this.bindSkuData(defaultSku)
                this.setStockStatus(defaultSku.stock)
            } else {
                this.bindSpuData()
            }
            this.bindFenceGroupData(fenceGroup)
            this.bindTipData()
        },
        bindSpuData() {
            const spu = this.properties.spu
            this.setData({
                previewImg: spu.img,
                title: spu.title,
                price: spu.price,
                discount_price: spu.discount_price
            })
        },
        bindSkuData(sku) {
            this.setData({
                previewImg: sku.img,
                title: sku.title,
                price: sku.price,
                discount_price: sku.discount_price,
                stock: sku.stock
            })
        },
        // 更新规格矩阵
        bindFenceGroupData(fenceGroup) {
            this.setData({
                fences: fenceGroup.fences
            })
        },
        // sku描述数据
        bindTipData() {
            this.setData({
                skuIntact: this.data.judger.isSkuIntact(),
                currentValues: this.data.judger.getCurrentValues(),
                missingKeys: this.data.judger.getMissingKeys()
            })
        },
        onCellTap(event) {
            let {cell, x, y} = event.detail
            const newCell = new Cell(cell.spec)
            newCell.status = cell.status
            const judger = this.data.judger
            judger.judge(newCell, x, y)
            // 每次点击都要判断规格是否选择完毕
            const skuIntact = judger.isSkuIntact()
            if (skuIntact) {
                // 选择完毕就更新sku数据
                const currentSku = judger.getDeterminateSku()
                this.bindSkuData(currentSku)
                this.setStockStatus(currentSku.stock)
            }
            this.bindTipData()
            this.bindFenceGroupData(judger.fenceGroup)
            this.triggerSpecEvent()
        },
        // 判断库存与购买数量,确定是否显示缺货
        setStockStatus(stock) {
            this.setData({
                outStock: stock < this.data.currentSkuCount
            })
        },
        onSelectCount(event) {
            this.data.currentSkuCount = event.detail.count

            if (Spu.isNoSpec(this.properties.spu)){
                this.setStockStatus(this._getNoSpecSku().stock)
            } else{
                if (this.data.judger.isSkuIntact()) {
                    const sku = this.data.judger.getDeterminateSku()
                    this.setStockStatus(sku.stock)
                }
            }
        },

        triggerSpecEvent() {
            const noSpec = Spu.isNoSpec(this.properties.spu)
            if (noSpec) {
                this.triggerEvent('specchange', {
                    noSpec
                })
            } else {
                // 自定义事件，向外传递对象
                this.triggerEvent('specchange', {
                    noSpec: noSpec,
                    skuIntact: this.data.judger.isSkuIntact(),
                    currentValues: this.data.judger.getCurrentValues(),
                    missingKeys: this.data.judger.getMissingKeys()
                })
            }
        },

        onBuyOrCart(event) {
            if (Spu.isNoSpec(this.properties.spu)) {
                this.shoppingNoSpec()
            } else {
                this.shoppingVarious()
            }
        },

        shoppingVarious() {
            const intact = this.data.judger.isSkuIntact()
            if (intact) {
                this._triggerShoppingEvent(this.data.judger.getDeterminateSku())
            } else {
                let missingKeys = this.data.judger.getMissingKeys();
                wx.showToast({
                    icon: 'none',
                    title: `请选择: ${missingKeys.join(',')}`,
                    duration: 3000
                })
            }
        },

        shoppingNoSpec() {
            this._triggerShoppingEvent(this._getNoSpecSku)
        },

        _getNoSpecSku() {
            return this.properties.spu.sku_list[0]
        },

        _triggerShoppingEvent(sku) {
            this.triggerEvent('shopping', {
                orderWay: this.properties.orderWay,
                spuId: this.properties.spu.id,
                sku: sku,
                skuCount: this.data.currentSkuCount
            })
        }

    }
})
